//
//  XMSendStatusController.m
//  XMWeibo
//
//  Created by chi on 14-8-29.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMSendStatusController.h"

#import "XMTextView.h"
#import "XMSendStatusToolBar.h"
#import "XMSendStatusTool.h"
#import "MBProgressHUD+XM.h"


@interface XMSendStatusController () <UITextViewDelegate, XMSendStatusToolDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

/** 发送按钮 */
@property (nonatomic, weak) UIBarButtonItem *sendBarButton;

/** 编辑框 */
@property (nonatomic, weak) XMTextView *textView;



@end

@implementation XMSendStatusController



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    
    //创建textview
    [self setupTextView];
    
    
    
    //导航按钮
    self.navigationItem.title = @"发微博";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(send)];
    self.sendBarButton = self.navigationItem.rightBarButtonItem;
    self.sendBarButton.enabled = NO;
    if (!isIOS7) {
        [self setBarButtonTextColor:self.sendBarButton];
        [self setBarButtonItemImage:self.sendBarButton];
    }
    
   
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.textView becomeFirstResponder];
}


#pragma mark - 发布
- (void)send
{
    // 请求参数
    XMHttpSendStatusParams *params = [XMHttpSendStatusParams params];
    params.status = self.textView.text;
    
    if (self.textView.images.count > 0) {
        // 带图片的微博
#warning 新浪只支持上传一张图片
        XMHttpFormData *formData = [XMHttpFormData formData];
        UIImage *image = [self.textView.images lastObject];
        formData.data = UIImageJPEGRepresentation(image, 0.5);
        
        [XMSendStatusTool sendStatusWithParam:params formDatas:@[formData] success:^(XMHttpSendStatusResult *result) {
            [MBProgressHUD showSuccess:@"发送微博成功"];
        } failure:^(NSError *error) {
            [MBProgressHUD showError:@"发送微博失败"];
            XMLog(@"--------> 发送微博失败 come here %@", error);
        }];
    }
    else {
        [XMSendStatusTool sendStatusWithParam:params success:^(XMHttpSendStatusResult *result) {
            [MBProgressHUD showSuccess:@"发送微博成功"];
        } failure:^(NSError *error) {
            [MBProgressHUD showError:@"发送微博失败"];
            XMLog(@"--------> 发送微博失败 come here %@", error);
        }];
    }
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark 处理toolbar

- (void)clickToolButton:(SendStatusToolButton)item
{
   
    switch (item) {
        case kSendStatusToolButtonCamera:
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc]init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            
            [self presentViewController:picker animated:YES completion:nil];
        }
            break;
        case kSendStatusToolButtonPiture:
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc]init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            
            [self presentViewController:picker animated:YES completion:nil];
        }
            break;
        case kSendStatusToolButtonMention:
            break;
        case kSendStatusToolButtonTrend:
            break;
        case kSendStatusToolButtonEmoticon:
            break;
        default:
            break;
    }
}

#pragma mark uiimagepickercontroller代理

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = info[@"UIImagePickerControllerOriginalImage"];
    
    [self.textView addImage:image];

    [picker dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - uitextview代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.textView resignFirstResponder];
}

#pragma mark - 创建textview
- (void)setupTextView
{
    XMTextView *textView = [[XMTextView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:textView];
    self.textView = textView;
    textView.placeholderText = @"分享新鲜事...";
    textView.delegate = self;
    
    //添加通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewTextChange:) name:UITextViewTextDidChangeNotification object:textView];
    
    XMSendStatusToolBar *toolView = [[XMSendStatusToolBar alloc]init];
    toolView.delegate = self;
    CGRect frame = self.view.bounds;
    frame.size.height = 44.0;
    toolView.frame = frame;
    self.textView.inputAccessoryView = toolView;
    
}

/**
 *  改变发送按钮的状态
 */
- (void)textViewTextChange:(NSNotification*)notification
{
    self.sendBarButton.enabled = [self.textView.text length] > 0;
}

#pragma mark - 取消
- (void)cancel
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


/**
 *  设置BarButtonItem字体颜色
 */
- (void)setBarButtonTextColor:(UIBarButtonItem*)item
{
    NSMutableDictionary *normalTextAtt = [NSMutableDictionary dictionary];
    normalTextAtt[UITextAttributeTextColor] = XMColor(255, 255, 255);
    normalTextAtt[UITextAttributeFont] = [UIFont systemFontOfSize:12.0];
    normalTextAtt[UITextAttributeTextShadowOffset] = [NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)];
    
    [item setTitleTextAttributes:normalTextAtt forState:UIControlStateNormal];
    
    NSMutableDictionary *disableTextAtt = [normalTextAtt mutableCopy];
    disableTextAtt[UITextAttributeTextColor] = [UIColor lightGrayColor];
    [item setTitleTextAttributes:disableTextAtt forState:UIControlStateDisabled];
}

/**
 *  设置BarButtonItem背景图片
 */
- (void)setBarButtonItemImage:(UIBarButtonItem*)item
{
    [item setBackgroundImage:[UIImage adaptIOS7ImageWithName:@"navigationbar_button_send_background"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [item setBackgroundImage:[UIImage adaptIOS7ImageWithName:@"navigationbar_button_send_background_pushed"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
    [item setBackgroundImage:[UIImage adaptIOS7ImageWithName:@"navigationbar_button_send_background_disable"] forState:UIControlStateDisabled barMetrics:UIBarMetricsDefault];
}

@end
